package com.zhuoshi.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zhuoshi.bean.ManagerRoleBean;
import com.zhuoshi.bean.ManagerRoleValueBean;
import com.zhuoshi.utils.DbHelper;
import com.zhuoshi.utils.RoleEnum;

/**
 * 角色表
 * @author telnet
 *
 */
public class ManagerRoleDao {
	
	
	//添加
	public int add(ManagerRoleBean bean){
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("insert into mt_manager_role (role_name,role_type,sys) values");
			sql.append(" (?,?,?)");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setString(1, bean.getRole_name());
			pstat.setInt(2, bean.getRole_type());
			pstat.setInt(3, bean.getSys());
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		
		return rows;
	}
	
	//修改
	public int update(ManagerRoleBean bean){
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("update mt_manager_role set");
			sql.append(" role_name=?,");
			sql.append(" role_type=?,");
			sql.append(" sys=?");
			sql.append(" where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setString(1, bean.getRole_name());
			pstat.setInt(2, bean.getRole_type());
			pstat.setInt(3, bean.getSys());
			pstat.setInt(4, bean.getId());
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		
		return rows;
	}
	
	//删除
	public int delete(int id){
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("delete from mt_manager_role ");
			sql.append(" where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		
		return rows;
	}
	
	//获取一个普通的RoleBean
	public ManagerRoleBean getManagerRoleBean(int id){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		ManagerRoleBean bean = null;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from mt_manager_role");
			sql.append(" where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			while(rs.next()){
				bean = new ManagerRoleBean();
				bean.setId(rs.getInt("id"));
				bean.setRole_name(rs.getString("role_name"));
				bean.setRole_type(rs.getInt("role_type"));
				bean.setSys(rs.getInt("sys"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		return bean;
	}
	
	
	
	
	
	
	
	/**
	 * 获取一个角色实体
	 * @param id
	 * @return
	 */
	public ManagerRoleBean getModel(int id) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		ManagerRoleBean bean = null;
		// #region 获取父表信息
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select id,role_name,role_type,sys from mt_manager_role where id=? limit 0,1");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			if (rs.next()) {
				bean = new ManagerRoleBean();
				bean.setId(rs.getInt("id"));
				bean.setRole_name(rs.getString("role_name"));
				bean.setRole_type(rs.getInt("role_type"));
				bean.setSys(rs.getInt("sys"));
			}

			// #endregion

			// #region 获取子表信息,通过role_id获取这个角色的 菜单nav_id 和 权限 action_type
			StringBuffer sql1 = new StringBuffer();
			sql1.append("select id,role_id,nav_id,action_type from mt_manager_role_value");
			if (bean.getRole_type() > 1) { // 如果role_type>1 就是普通类型 如果是1的话就是系统类型的
											// 也就是管理员role_type=1
				sql1.append(" where role_id="+bean.getId());
			}
			PreparedStatement pstat1 = conn.prepareStatement(sql1.toString());
			
			//System.out.println(sql1.toString());
			//System.out.println(bean.getRole_type());
			
			//pstat1.setInt(1, bean.getId());
			ResultSet rs1 = pstat1.executeQuery();
			ManagerRoleValueBean modelt = null;
			List<ManagerRoleValueBean> listRoleValue = new ArrayList<ManagerRoleValueBean>();
			while (rs1.next()) {
				modelt = new ManagerRoleValueBean();
				modelt.setId(rs1.getInt("id"));
				modelt.setRole_id(rs1.getInt("role_id"));
				modelt.setNav_id(rs1.getInt("nav_id"));
				modelt.setAction_type(rs1.getString("action_type"));
				if ((bean.getRole_type() > 1) && RoleEnum.SHOW.toString().equals(rs1.getString("action_type"))) {//如果role_type>1那么就属于普通管理类型  1是系统管理类型
					listRoleValue.add(modelt);
				} 
			}
			// #endregion

			bean.setManager_role_values(listRoleValue);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, rs);
		}

		return bean;
	}
	
	/**
	 * 判断数据是否存在
	 * @param id
	 * @return
	 */
	public boolean exists(int id){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs =null;
		
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select count(id) from mt_manager_role where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			if(rs.last()){
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		return false;
	}
	/**
	 * 通过id获取角色名称
	 * @param id
	 * @return
	 */
	public String getRoleName(int id){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		String outStr = "";
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select role_name from mt_manager_role where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			if(rs.next()){
				outStr = rs.getString("role_name");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		
		return outStr;
	}
	
	public List<ManagerRoleBean> getList(){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		List<ManagerRoleBean> list = new ArrayList<ManagerRoleBean>();
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from mt_manager_role");
			pstat = conn.prepareStatement(sql.toString());
			rs = pstat.executeQuery();
			while(rs.next()){
				ManagerRoleBean bean = new ManagerRoleBean();
				bean.setId(rs.getInt("id"));
				bean.setRole_name(rs.getString("role_name"));
				bean.setRole_type(rs.getInt("role_type"));
				bean.setSys(rs.getInt("sys"));
				list.add(bean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		return list;
	}
	
	/**
	 * 通过id获取role_type
	 */
	public Integer getRoleType(int id){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		int role_type = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select role_type from mt_manager_role");
			sql.append(" where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			if(rs.next()){
				role_type = rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		return role_type;
	}
	
}
